﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

public partial class Controls_Page_General : System.Web.UI.UserControl
{
    class_database db;
    //string idMau = "0";
    class_treeview treeView;
    public string rawURL = HttpContext.Current.Request.ServerVariables["URL"];
    
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["idLoaiMau"] != null)
        {
            string idLoaiMau = Convert.ToString(class_string.clear_inject(Request.QueryString["idLoaiMau"].Trim()));
            if (idLoaiMau != "0")
            {
                string idFirstMau = GetIDFirstMau(idLoaiMau);
                Session["idMau"] = idFirstMau;
                Session["nameBV"] = "ThongTinChung";
            }
            btnThemMau.Attributes.Add("style", "display: inline-block");
        } 
        else if (Request.QueryString["idMau"] != null && Request.QueryString["nameBV"] == null)
        {
            string idMau = Convert.ToString(class_string.clear_inject(Request.QueryString["idMau"].Trim()));
            if (idMau != "0")
            {
                Session["idMau"] = idMau;
                Session["nameBV"] = "ThongTinChung";
            }
            btnThemBaiViet.Attributes.Add("style", "display: inline-block");
        }
        else if (Request.QueryString["idMau"] != null && Request.QueryString["nameBV"] != null)
        {
            string nameBV = Convert.ToString(class_string.clear_inject(Request.QueryString["nameBV"].Trim()));
            string idMau = Convert.ToString(class_string.clear_inject(Request.QueryString["idMau"].Trim()));
            if (nameBV != "" && idMau != "")
            {
                Session["idMau"] = idMau;
                Session["nameBV"] = nameBV;
            }
        }
        else
        {
            Session["idMau"] = "1";
            Session["nameBV"] = "ThongTinChung";
        }
        FillTree();
        GetWorkPlace();
        LoadCate();
    }

    protected string GetIDFirstMau(string idLoaiMau)
    {
        string idFirst = "1";
        string sql = "SELECT id FROM tbl_mau WHERE type = " + idLoaiMau + " ORDER BY name LIMIT 1";
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            idFirst = dt.Rows[0]["id"].ToString();
        }
        return idFirst;
    }

    protected void LoadCate()
    {
        Control ct = null;
        HtmlGenericControl gel = new HtmlGenericControl("div");

        string nameBVSession = Convert.ToString(Session["nameBV"]);
        if (nameBVSession == "")
        {
            nameBVSession = "ThongTinChung";
        }
        ct = Page.LoadControl("~/Controls/Template/" + nameBVSession + ".ascx");
        gel.Controls.Add(ct);
        placeHolder2.Controls.Clear();
        placeHolder2.Controls.Add(gel);
    }

    protected void GetWorkPlace()
    {
        if (Request.QueryString["idLoaiMau"] != null)
        {
            string idLoaiMau = Convert.ToString(class_string.clear_inject(Request.QueryString["idLoaiMau"].Trim()));
            if (idLoaiMau != "0")
            {
                string sql = "SELECT name FROM tbl_loaimau WHERE id = " + idLoaiMau;
                db = new class_database();
                DataTable dt = db.GetDataTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    lbWorkPlace.ForeColor = System.Drawing.Color.Red;
                    lbWorkPlace.Font.Name = "Comic Sans MS";
                    lbWorkPlace.Text = "Place: " + dt.Rows[0]["name"].ToString();
                }
            }
        }
        else if (Request.QueryString["idMau"] != null && Request.QueryString["nameBV"] == null)
        {
            string idMau = Convert.ToString(class_string.clear_inject(Request.QueryString["idMau"].Trim()));
            if (idMau != "0")
            {
                string sql = "SELECT tbl_loaimau.name as nameLoaiMau, tbl_mau.name as nameMau FROM tbl_loaimau, tbl_mau WHERE tbl_loaimau.id = tbl_mau.type AND tbl_mau.id = " + idMau;
                db = new class_database();
                DataTable dt = db.GetDataTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    lbWorkPlace.ForeColor = System.Drawing.Color.Red;
                    lbWorkPlace.Font.Name = "Comic Sans MS";
                    lbWorkPlace.Text = "Place: " + dt.Rows[0]["nameLoaiMau"].ToString() + "/" + dt.Rows[0]["nameMau"].ToString();
                }
            }
        }
        else if (Request.QueryString["idMau"] != null && Request.QueryString["nameBV"] != null)
        {
            string nameBV = Convert.ToString(class_string.clear_inject(Request.QueryString["nameBV"].Trim()));
            string idMau = Convert.ToString(class_string.clear_inject(Request.QueryString["idMau"].Trim()));
            if (nameBV != "" && idMau != "0")
            {
                string placeBaiViet = "";
                if (nameBV == "ThongTinChung")
                {
                    placeBaiViet = "Thông Tin Chung";
                }
                else if (nameBV == "ThongTinFile")
                {
                    placeBaiViet = "Thông Tin File";
                } else if (nameBV == "ThongTinHamDiet")
                {
                    placeBaiViet = "Thông Tin Hàm Diệt";
                } else if(nameBV == "ThongTinKyThuatChiTiet") 
                {
                    placeBaiViet = "Thông Tin Kỹ Thuật Chi Tiết";
                }
                string sql = "SELECT tbl_loaimau.name as nameLoaiMau, tbl_mau.name as nameMau FROM tbl_loaimau, tbl_mau WHERE tbl_loaimau.id = tbl_mau.type AND tbl_mau.id = " + idMau;
                db = new class_database();
                DataTable dt = db.GetDataTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    lbWorkPlace.ForeColor = System.Drawing.Color.Red;
                    lbWorkPlace.Font.Name = "Comic Sans MS";
                    lbWorkPlace.Text = "Place: " + dt.Rows[0]["nameLoaiMau"].ToString() + "/" + dt.Rows[0]["nameMau"].ToString() + "/" + placeBaiViet;
                }
            }
        }
    }

    protected void btnThemMau_Click(object sender, EventArgs e)
    {

    }

    protected void btnThemBaiViet_Click(object sender, EventArgs e)
    {
        string idMau = Convert.ToString(class_string.clear_inject(Request.QueryString["idMau"].Trim()));

    }

    protected void searchVirus(object sender, ImageClickEventArgs e)
    {
        if (tbVirusSearch.Text != "")
        {
            string keySearch = tbVirusSearch.Text;
            string sql = "SELECT id, name FROM tbl_mau WHERE name LIKE '%" + keySearch + "%'";
            db = new class_database();
            DataTable dt = db.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                Response.Redirect("Default.aspx?module=Template&view=General&idMau=" + dt.Rows[0]["id"].ToString());
                //FindByText(dt.Rows[0]["name"].ToString());
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

    }

    //protected void GetSession(string idMau, string idBaiViet)
    //{

    //}
















    protected void FillTree()
    {
        treeView = new class_treeview();
        DataTable dt = treeView.getTreeData();
        treeView1.Nodes.Clear();
        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode tnLoaiMau = new TreeNode();
                tnLoaiMau.Text = dt.Rows[i]["name"].ToString();
                tnLoaiMau.Value = dt.Rows[i]["id"].ToString();
                string value = dt.Rows[i]["id"].ToString();
                //tnLoaiMau.Collapse();
                tnLoaiMau.SelectAction = TreeNodeSelectAction.SelectExpand;
                tnLoaiMau.Expand();
                tnLoaiMau.Selected = true;
                string url = rawURL + "?module=Template&view=General&idLoaiMau=" + dt.Rows[i]["id"].ToString();
                tnLoaiMau.NavigateUrl = url;
                treeView1.Nodes.Add(tnLoaiMau);
                FillChild(tnLoaiMau, value);
            }
        }
    }


    public void FillChild(TreeNode parent, string parentID)
    {
        db = new class_database();
        treeView = new class_treeview();
        DataTable dt = treeView.getTreeNodeData(parentID);
        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode tnMau = new TreeNode();
                tnMau.Text = dt.Rows[i]["name"].ToString();
                tnMau.Value = dt.Rows[i]["id"].ToString();
                string value = dt.Rows[i]["id"].ToString();
                tnMau.Collapse();
                //tnMau.SelectAction = TreeNodeSelectAction.SelectExpand;
                //tnMau.Expand();
                //tnMau.Selected = true;
                string url = rawURL + "?module=Template&view=General&idMau=" + value;
                tnMau.NavigateUrl = url;
                parent.ChildNodes.Add(tnMau);

                string sql = "SELECT tbl_loaibaiphantich.name, tbl_baiphantich.id FROM tbl_baiphantich, tbl_loaibaiphantich"
                    + " WHERE tbl_baiphantich.idloaibai = tbl_loaibaiphantich.id AND tbl_baiphantich.idmau = " + value;
                DataTable dt2 = db.GetDataTable(sql);
                if (dt2 != null && dt2.Rows.Count > 0)
                {
                    for (int j = 0; j < dt2.Rows.Count; j++)
                    {
                        TreeNode tnBaiViet = new TreeNode();
                        tnBaiViet.Text = dt2.Rows[j]["name"].ToString();
                        string text = tnBaiViet.Text;
                        tnBaiViet.Value = dt2.Rows[j]["id"].ToString();
                        tnBaiViet.Expand();
                        if (tnBaiViet.Text != "Mẫu + Pdb + Hàm Diệt + Data")
                        {
                            string stringCate = "";
                            if (text == "Thông Tin Chung")
                            {
                                stringCate = "ThongTinChung";
                            }
                            else if (text == "Thông Tin Kỹ Thuật Chi Tiết")
                            {
                                stringCate = "ThongTinKyThuatChiTiet";
                            }
                            else if (text == "Thông Tin File")
                            {
                                stringCate = "ThongTinFile";
                            }
                            else if (text == "Thông Tin Hàm Diệt")
                            {
                                stringCate = "ThongTinHamDiet";
                            }
                            //Session["nameBV"] = stringCate;
                            string urlBaiViet = rawURL + "?module=Template&view=General&nameBV=" + stringCate + "&idMau=" + tnMau.Value;
                            tnBaiViet.NavigateUrl = urlBaiViet;

                        }
                        tnMau.ChildNodes.Add(tnBaiViet);
                    }
                }
            }
        }
    }

    private void FindByText(string nameMau)
    {
        TreeNodeCollection nodes = treeView1.Nodes;
        foreach (TreeNode n in nodes)
        {
            FindRecursive(n, nameMau);
        }
    }

    private void FindRecursive(TreeNode treeNode, string nameMau)
    {
        foreach (TreeNode tn in treeNode.ChildNodes)
        {
            tn.CollapseAll();
            if (tn.Text == nameMau)
            {
                //Response.Redirect("Default.aspx?module=Template&view=General&idMau=" + idMau);
                tn.Expand();
                tn.Selected = true;
                tn.SelectAction = TreeNodeSelectAction.SelectExpand;
            }
            //FindRecursive(tn, nameMau);
        }
        //oldKeySearchVirus = nameMau;
    }
}